package cn.yanglq.Rss.web.interceptor;

import cn.yanglq.Rss.web.WebConsts;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        /**
         *
         * 页面缓存设定 确保浏览器不缓存页面数据
         */
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "no-cache");

        String url = request.getRequestURI();
        System.out.println("访问地址：" + url);

        HttpSession session = request.getSession();

        if (session.getAttribute(WebConsts.SESSION_USERID) != null) {
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/error/redirect.jsp");
            return false;
        }
    }

}
